import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author 苏文广 created at 2018/12/11
 * @Description: joda demo
 */
public class DemoTest {
    private static final String DATETIME_PATTERN="yyyy-mm-dd HH:mm:ss";
    private static final String DATE_PATTERN="yyyy-mm-dd";
    private static final String TIME_PATTERN="HH:mm:ss";
    /**
     * String转换为joda-time
     */
    @Test
    public void testStrToJodaDate(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATETIME_PATTERN);

        DateTime parse = DateTime.parse("2018-12-11 17:06:30", dateTimeFormatter);

        System.out.println(parse);
    }

    /**
     * date 转 joda-time
     */
    @Test
    public void testDateToJodaDate(){
        Date date = new Date();

        DateTime dateTime = new DateTime(date);

        System.out.println(dateTime);

    }

    /**
     * calendar 转 joda-time
     */
    @Test
    public void testCalendarToJodaDate(){
        Calendar instance = Calendar.getInstance();

        DateTime dateTime = new DateTime(instance);

        System.out.println(dateTime);
    }

    /**
     * string 转 date
     */
    @Test
    public void testStrToDate(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_PATTERN);

        String str="2018-12-07";

        DateTime parse = DateTime.parse(str, dateTimeFormatter);

        Date date = parse.toDate();

        System.out.println(date);

    }

    /**
     * joda 转 calendar
     */
    @Test
    public void testJodaToCalendar(){
        DateTime dateTime = new DateTime(new Date());

        Calendar calendar = dateTime.toCalendar(Locale.CHINA);
        System.out.println(calendar);
    }

    /**
     * joda 转 str
     */
    @Test
    public void testJodaToStr(){
        DateTime dateTime = new DateTime();
        String string = dateTime.toString(TIME_PATTERN);
        String string2 = dateTime.toString(TIME_PATTERN,Locale.CHINA);
        System.out.println(string+string2);
    }

    /**
     * date 转 str
     */
    @Test
    public void testDateToStr(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);
        String format = simpleDateFormat.format(date);
        System.out.println(format);
    }
}
